from flask import Blueprint, request
from models.entity.sensor_group import SensorGroup
from models.entity.sensor import Sensor
import json
from utils.result import result

mod = Blueprint('core_sensor_group', __name__, url_prefix='/core/sensor_group', template_folder='templates',
                static_folder='static')


@mod.route('/add/', methods=['POST'])
def add_sensor_group():
    """增加传感器分组"""
    """
    {
    'name':'光线传感器组',
    'dis_order':1,
    'description':'wu'
    }
    """
    try:
        dict=json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    if "id" in dict:
        dict.pop("id")
    if SensorGroup.query.filter_by(name=dict["name"]).first() is not None:
        return result(success=0, error_code=-2, error_msg="传感器分组名称已被占用")
    sensor_group = SensorGroup()
    try:
        sensor_group.from_dict(dict)
    except RuntimeError as e:
        return result(success=0, error_code=-1, error_msg=str(e))
    if sensor_group.save():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/edit/', methods=[ 'POST'])
def edit_sensor_group():
    """编辑传感器分组"""
    """
    {
    'id':1,
    'name':'气器分组',
    'dis_order':2,
    'description':'wu'
    }
    """
    try:
        dict=json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    sensor_group = SensorGroup.query.filter_by(id=dict["id"]).first()
    if not sensor_group:
        return result(success=0, error_code=-2, error_msg="指定传感器异常")
    if dict["name"] != sensor_group.name and SensorGroup.query.filter_by(name=dict["name"]).first():
        return result(success=0, error_code=-2, error_msg="传感器名称已被占用")
    try:
        sensor_group.from_dict(dict)
    except RuntimeError as e:
        return result(success=0, error_code=-2, error_msg=str(e))
    if sensor_group.save():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/del/', methods=['GET', 'POST'])
def del_sensor_group():
    """删除传感器分组"""
    """
    {
    'id':1
    }
    """
    try:
        dict=json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    # 检查此传感器组下是否还有传感器
    if Sensor.query.filter_by(sensor_group_id=dict["id"]).first():
        return result(success=0, error_code=-2, error_msg="该传感器分组下还有传感器，不能删除")
    sensor_group = SensorGroup.query.filter_by(id=dict["id"]).first()
    if sensor_group.delete():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/get_all/', methods=['GET', 'POST'])
def get_sensor_group():
    """获取所有传感器分组"""
    """
        无需传入参数
    """
    sensor_groups = SensorGroup.get_sensor_group_all()
    data = [item.to_dict() for item in sensor_groups]
    return result(success=1, data=data)